#!/bin/bash
#2022-01-05 cnetos7+
Core=`cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l`
Geturl="https://mirror.cnop.net"
echo "Cpu core number $Core"
echo -e "\033[32m Enter a nginx path( Default /usr/local/node ):   \033[0m"
read Dir
if [ -z "$Dir" ]; then
  echo -e "\033[32m  Using the default path /usr/local/node :\033[0m"
  Dir=/usr/local/node
  echo $Dir
else
  echo -e "\033[32m Your input $Path :\033[0m"
  echo $Dir
fi

if [ -e $Dir ]; then
#Folder name exists in system
echo -e "\033[32m Ready to Install.   \033[0m"
else
#Folder name no exists in system
echo -e "\033[32m Create $Dir   \033[0m"
mkdir $Dir
fi

#nodejs_version
echo " " && echo -e "\033[32m Please enter a nodejs version using Numbers instead: \033[0m"
echo -e "\033[32m 1.node-v14.9.0 (Default)  \033[0m"
echo -e "\033[32m 2.node-v16.13.1    \033[0m"
#echo -e "\033[32m 3.openresty-1.19.3.1   \033[0m"
read Vname
case $Vname in
1|'')
    echo node-v14.9.0
	Vname=node-v14.9.0
    ;;
2)
    echo node-v16.13.1
	Vname=node-v16.13.1
    ;;
# 3)
    # echo openresty-1.19.3.1
	# Vname=openresty-1.19.3.1
    # ;;
esac

echo -e "\033[32m Installing libraries... \033[0m"
yum -y install epel*  python3 && yum install -y centos-release-scl && yum install -y devtoolset-8-gcc*
echo "source /opt/rh/devtoolset-8/enable" >> /etc/profile && source /etc/profile && gcc --version

echo -e "\033[32m Download $Vname.tar.gz: \033[0m"
cd /home  && wget $Geturl/nodejs/$Vname.tar.gz 
tar zxvf $Vname.tar.gz && cd $Vname && echo "$Vname"
#nake && make install
./configure --prefix=$Dir
make -j$Core && sudo make install -j$Core

ln -s $Dir/bin/node /usr/bin/node

echo '
#set for nodejs
export NODE_HOME=/usr/local/node
export PATH=$NODE_HOME/bin:$PATH
'>>/etc/profile
source /etc/profile


node -v && npm -v 


sleep 3


if [ -e $Dir/bin/node ]; then
#succeed
echo -e "\033[32m Installation success.   \033[0m"
else
#Folder name no exists in system
echo -e "\033[32m Installation failed.   \033[0m"
mkdir $Dir
fi